<?php
 
class Pages extends MY_Controller 
{
	var $curl = 'pages';
	var $name = 'Pages Manager';
	var $search = '';
	var $language;
	var $submenu = "_menu";
	var $id_category;
	var $category_alias;

	function __construct()
	{
		parent::__construct();
        
        $this->load->model('/site/page_model');
        
		$this->validation->set_fields($this->page_model->fields);

		$this->data['Title'] = T_('Pages Manager');

		$this->language = parent::getLang();
		
		$this->name = T_('Pages Manager');		
    }
	
	function index($category_alias)
	{		
		$this->category_alias = $category_alias;
		$this->id_category = $this->getCategoryByAlias($this->category_alias);
		
		$this->data['Title'] = $this->page_model->category[ $this->id_category ];
		
		$this->page_model->search();
		//$this->search = $this->data['search'];
		
		$this->renderLayout();
    }
	function book_info()
	{		
		$this->renderLayout();
    }
	function create($category_alias)
	{
		$this->category_alias = $category_alias;
		$this->id_category = $this->getCategoryByAlias($this->category_alias);
		
        if (count($_POST))
		{
			$rules = array('title', 'alias', 'teaser', 'text', 'meta_title', 'meta_description', 'meta_keywords', 'status');
			$this->validation->set_rules($this->page_model->getRules($rules));

			if ($this->validation->run() === TRUE)
			{							
				$item['title'] 		= $this->validation->title;
				$item['alias'] 		= $this->validation->alias;
				$item['teaser'] 	= $this->validation->teaser;
				$item['text'] 		= $this->validation->text;
				$item['status'] 	= $this->validation->status;
				$item['id_admins'] 	= $this->auth->getID();
				$item['category'] 	= $this->id_category;
				$item['meta_title'] 		= $this->validation->meta_title;				
				$item['meta_description'] 	= $this->validation->meta_description;
				$item['meta_keywords'] 		= $this->validation->meta_keywords;				
				
				if ($this->page_model->insert($item))
				{
					redirect($this->curl . '/index/'. $this->category_alias);	
				}				
			}
		}

		$this->data['Title'] = $this->page_model->category[ $this->id_category ] .' / '. T_('Создать');
		$this->data['wysiwyg'] = true;
		
		$this->renderLayout();        
    }
    
	function edit($id, $category_alias)
	{
		$this->category_alias = $category_alias;
		$this->id_category = $this->getCategoryByAlias($this->category_alias);
		
		if (!empty($id))
		{
			$object = $this->page_model->get($id);
			
			if (!count($object))
			{
				$this->data['view'] = '_elements/query_no_object';
				$this->load->view($this->data['layout'], $this->data);
				return false; 
			}
		
			if (!count($_POST))
			{
				$this->validation->id 		= $object['id']; 
				$this->validation->title 	= $object['title'];
				$this->validation->alias 	= $object['alias'];
	        	$this->validation->teaser 	= $object['teaser'];
	        	$this->validation->text 	= $object['text']; 	        	
	        	$this->validation->meta_title 		= $object['meta_title'];
	        	$this->validation->meta_description = $object['meta_description'];
	        	$this->validation->meta_keywords 	= $object['meta_keywords'];		
	        	$this->validation->status 	= $object['status'];	
			}
			else
			{
				$rules = array('title', 'alias', 'teaser', 'text', 'meta_title', 'meta_description', 'meta_keywords', 'status');
				$this->validation->set_rules($this->page_model->getRules($rules));
	
				if ($this->validation->run() === TRUE)
				{
					$item['id'] 		= $this->validation->id;
					$item['title'] 		= $this->validation->title;
					$item['alias'] 		= $this->validation->alias;
					$item['teaser'] 	= $this->validation->teaser;
					$item['text'] 		= $this->validation->text;
					$item['status'] 	= $this->validation->status;
					//$item['id_admins'] 	= $this->auth->getID();
					$item['category'] 	= $this->id_category;
					$item['meta_title'] 		= $this->validation->meta_title;				
					$item['meta_description'] 	= $this->validation->meta_description;
					$item['meta_keywords'] 		= $this->validation->meta_keywords;															
					$item['date_modified'] = date('Y-m-d H:i:s');
					
					$this->page_model->update($item);					
					
					redirect($this->curl . '/index/'. $this->category_alias);				
				}
			}			
		}
           
		$this->data['Title'] = $this->page_model->category[ $this->id_category ] .' / '. T_('Edit');		
		$this->data['wysiwyg'] = true; 
		
		$this->renderLayout();
    }
        
    function insert_category()
    {
        $page_name = trim($this->input->post('categoryname'));
        $parentid = (int)$this->input->post('categoryid');
        $parentNode = $this->cats->getNodeFromId($parentid);
        
        $fields_array = array("categoryname"=>$page_name);
        $this->cats->appendNewChild($parentNode,$fields_array);
        
        $this->_render_page();
    }
        
	function delete($id, $category_alias)
	{
		$data = $this->page_model->delete($id);

		if ($data)
		{
			redirect($this->curl . '/index/'. $category_alias);	
		}
		else
		{
			redirect($this->curl . '/edit/'. $id . '/' . $category_alias);
		}
	}
	        
	function deleteItems()
	{
		$data = $this->page_model->delete($_REQUEST['items']);

		echo $data;
	}
	    
	function activity()
	{
		$data = $this->utils->publishItems($this->article_model->table, $this->article_model->prKey, $this->article_model->publishedField, $_REQUEST['items'], $_REQUEST['action']);
		echo $this->utils->php2js($data);
	}

	function getCategoryByAlias($category_alias)
	{	
		$id_category = '';
		
		if (!empty($category_alias))
		{
			if ($category_alias == 'static')
			{
				$id_category = 1;	
			}
			else if ($category_alias == 'news')
			{
				$id_category = 2;	
			}	
		}
		
		return $id_category;
	}
	
	/*
	 * Static Pages 
	*/
	function view($page_alias, /*$topic,*/ $id_category = PAGE_CATEGORY_STATIC)
	{
		/*
		$this->id_category = $id_category;
		$this->data['Title'] = '';
		
		$this->data['page'] = $this->page_model->getByAlias($page_alias);
		
		if (!empty($this->data['page']))
		{
			$this->data['Title'] = $this->data['page']->title;
		}
		*/
		
		$this->view = '/pages/'.$page_alias;//.'/'.$topic;
        $object = $this->page_model->getByAlias($page_alias);
        $this->data['staticPage'] = (is_object($object)) ? $object->text : '';
        $this->renderLayout();	
	}
	
	/*
	 * News Pages 
	*/
	function news($page_alias = 'all', $id_category = PAGE_CATEGORY_ADVERTISER)
	{
		$this->id_category = $id_category;
		$this->data['Title'] = '';
		
		if($page_alias == 'all')
		{
			$this->data['news_all'] = $this->page_model->getByAlias();
			$this->data['Title'] = T_('Все новости');
			$this->view = '/pages/news';
		}
		else
		{
			$this->data['page'] = $this->page_model->getByAlias($page_alias);
			
			if (!empty($this->data['page']))
			{
				$this->data['Title'] = $this->data['page']->title;
			}
			
			$this->view = '/pages/view';
		}
		
		$this->renderLayout();	
	}
}

?>